using System;
using System.Drawing;
using System.Globalization;
using System.Reflection;
using System.Xml;
namespace ICSharpCode.TextEditor.Document
{
    public class HighlightColor
    {
        private Color color;
        private Color backgroundcolor = Color.WhiteSmoke;
        private bool bold;
        private bool italic;
        private bool hasForeground;
        private bool hasBackground;
        public bool HasForeground
        {
            get
            {
                return this.hasForeground;
            }
        }
        public bool HasBackground
        {
            get
            {
                return this.hasBackground;
            }
        }
        public bool Bold
        {
            get
            {
                return this.bold;
            }
        }
        public bool Italic
        {
            get
            {
                return this.italic;
            }
        }
        public Color BackgroundColor
        {
            get
            {
                return this.backgroundcolor;
            }
        }
        public Color Color
        {
            get
            {
                return this.color;
            }
        }
        public Font GetFont(FontContainer fontContainer)
        {
            if(this.Bold)
            {
                if(!this.Italic)
                {
                    return fontContainer.BoldFont;
                }
                return fontContainer.BoldItalicFont;
            }
            else
            {
                if(!this.Italic)
                {
                    return fontContainer.RegularFont;
                }
                return fontContainer.ItalicFont;
            }
        }
        private Color ParseColorString(string colorName)
        {
            string[] array = colorName.Split(new char[]
			{
				'*'
			});
            PropertyInfo property = typeof(SystemColors).GetProperty(array[0], BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
            Color result = (Color)property.GetValue(null, null);
            if(array.Length == 2)
            {
                double num = double.Parse(array[1]) / 100.0;
                result = Color.FromArgb((int)((double)result.R * num), (int)((double)result.G * num), (int)((double)result.B * num));
            }
            return result;
        }

        private Color PaserColorFrom(string text)
        {
            if(text[0] == '#') return HighlightColor.ParseColor(text);

            if(text.Contains(","))
            {
                var s = text.Split(',');
                if(s.Length == 3) return Color.FromArgb(int.Parse(s[0]), int.Parse(s[1]), int.Parse(s[2]));

                if(s.Length == 4) return Color.FromArgb(int.Parse(s[0]), int.Parse(s[1]), int.Parse(s[2]), int.Parse(s[3]));

                throw new ArgumentOutOfRangeException("color");
            }

            if(text.StartsWith("SystemColors.")) return this.ParseColorString(text.Substring("SystemColors.".Length));

            return (Color)this.Color.GetType().InvokeMember(text, BindingFlags.GetProperty, null, this.Color, new object[0]);


        }
        public HighlightColor(XmlElement el)
        {
            if(el.Attributes["bold"] != null)
            {
                this.bold = bool.Parse(el.Attributes["bold"].InnerText);
            }
            if(el.Attributes["italic"] != null)
            {
                this.italic = bool.Parse(el.Attributes["italic"].InnerText);
            }
            if(el.Attributes["color"] != null)
            {
                string innerText = el.Attributes["color"].InnerText;
                this.color = this.PaserColorFrom(innerText);
                this.hasForeground = true;
            }
            else
            {
                this.color = Color.Transparent;
            }
            if(el.Attributes["bgcolor"] != null)
            {
                string innerText2 = el.Attributes["bgcolor"].InnerText;
                this.backgroundcolor = this.PaserColorFrom(innerText2);
                this.hasBackground = true;
            }
        }
        public HighlightColor(XmlElement el, HighlightColor defaultColor)
        {
            if(el.Attributes["bold"] != null)
            {
                this.bold = bool.Parse(el.Attributes["bold"].InnerText);
            }
            else
            {
                this.bold = defaultColor.Bold;
            }
            if(el.Attributes["italic"] != null)
            {
                this.italic = bool.Parse(el.Attributes["italic"].InnerText);
            }
            else
            {
                this.italic = defaultColor.Italic;
            }
            if(el.Attributes["color"] != null)
            {
                string innerText = el.Attributes["color"].InnerText;
                this.color = this.PaserColorFrom(innerText);
                this.hasForeground = true;
            }
            else
            {
                this.color = defaultColor.color;
            }
            if(el.Attributes["bgcolor"] != null)
            {
                string innerText2 = el.Attributes["bgcolor"].InnerText;
                this.backgroundcolor = this.PaserColorFrom(innerText2);
                this.hasBackground = true;
                return;
            }
            this.backgroundcolor = defaultColor.BackgroundColor;
        }
        public HighlightColor(Color color, bool bold, bool italic)
        {
            this.hasForeground = true;
            this.color = color;
            this.bold = bold;
            this.italic = italic;
        }
        public HighlightColor(Color color, Color backgroundcolor, bool bold, bool italic)
        {
            this.hasForeground = true;
            this.hasBackground = true;
            this.color = color;
            this.backgroundcolor = backgroundcolor;
            this.bold = bold;
            this.italic = italic;
        }
        public HighlightColor(string systemColor, string systemBackgroundColor, bool bold, bool italic)
        {
            this.hasForeground = true;
            this.hasBackground = true;
            this.color = this.ParseColorString(systemColor);
            this.backgroundcolor = this.ParseColorString(systemBackgroundColor);
            this.bold = bold;
            this.italic = italic;
        }
        public HighlightColor(string systemColor, bool bold, bool italic)
        {
            this.hasForeground = true;
            this.color = this.ParseColorString(systemColor);
            this.bold = bold;
            this.italic = italic;
        }
        private static Color ParseColor(string c)
        {
            int alpha = 255;
            int num = 0;
            if(c.Length > 7)
            {
                num = 2;
                alpha = int.Parse(c.Substring(1, 2), NumberStyles.HexNumber);
            }
            int red = int.Parse(c.Substring(1 + num, 2), NumberStyles.HexNumber);
            int green = int.Parse(c.Substring(3 + num, 2), NumberStyles.HexNumber);
            int blue = int.Parse(c.Substring(5 + num, 2), NumberStyles.HexNumber);
            return Color.FromArgb(alpha, red, green, blue);
        }
        public override string ToString()
        {
            return string.Concat(new object[]
			{
				"[HighlightColor: Bold = ", 
				this.Bold, 
				", Italic = ", 
				this.Italic, 
				", Color = ", 
				this.Color, 
				", BackgroundColor = ", 
				this.BackgroundColor, 
				"]"
			});
        }
    }
}
